#! /usr/bin/env python

# programmed by Critter, for the lulz. Long time ago. Requires python3.

import random
import sys

# Code comprising your turn, *after* you bet

def your_turn(total,cash,aitotal):
    switch = 1
    while switch == 1:
        print(total)
        choice = input("Hit or pass? ".lower())
        # HIT
        if choice == "hit":
            newcard = random.randint(1,10)
            total += newcard
            if total > 21:
                switch = 0
                print("Bust! Better luck next time.\n")
                cash -= bet
            if total == 21:
                print("Ding ding ding ding! You win!")
                cash += bet
                switch = 0
        # PASS
        elif choice == "pass":
            if (aitotal <= 21 and total > aitotal) or aitotal > 21:
                print("Ding ding ding ding! You win!")
                cash+= bet
            elif aitotal < 21 and total <= aitotal:
                print("You got PWNT!\n")
                cash -= bet
            switch = 0
        # LEAVE
        elif choice == "leave":
            sys.exit()
        else:
            print("Hit or pass, dude, it's not that hard to understand!")
            
    return cash


# AI CODE (ZOMG, THE MATRIX!)

def ai_turn(aitotal):
    while aitotal < 17:
        aitotal += random.randint(1,10)
    return aitotal
        

# BEGIN MAIN CODE SHIT

print("Welcome to this awesome hand of blackjack!")
print("Place your bets and get on with it!")

cash = 100

while True:
    #dealer goes
    card1 = random.randint(1,10)
    card2 = random.randint(1,10)
    aitotal = card1 + card2

    #player goes
    card1 = random.randint(1,10)
    card2 = random.randint(1,10)
    total = card1 + card2
    
    while True:
        print("Funds: ${}".format(cash))
        bet = input("Place your bet! ")
        if bet.isdigit():
            bet = int(bet)
            break
        else:
            print("only didgets, please!")
    
    aitotal = ai_turn(aitotal)
    cash = your_turn(total,cash,aitotal)